define(["jquery","bbcare","comm/form/decode"],function($,$bb,$decode){
	var quest = {};

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_whz" type="text-html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} class="qaCard-whz qaCard form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'   	<div class="control-input">'+
				'			{{if status=="default"}}'+
				'   			<input type="text" class="form-control validatebox" name="{{name}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" placeholder="{{placeHolder}}" data-validate="{{validate}}" data-attention="{{attention}}" autocomplete="off">'+
				'			{{else if status=="readonly"}}'+
				'				<p class="form-control-static validatebox" name="{{name}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}"></p>'+
				'			{{else if status=="disabled"}}'+
				'   			<input type="text" class="form-control validatebox" name="{{name}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" placeholder="{{placeHolder}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'			{{/if}}'+
				'		</div>'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			$dom.hasClass("form-control-static") ? $dom.html(value.quesValue) : $dom.val(value.quesValue);
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var value = {};
		value.quesValue = $dom.hasClass("form-control-static") ? $dom.html() : $dom.val();
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		var dataUnit = $dom.attr("data-dataUnit");
		if( dataUnit ) value.dataUnit = dataUnit;
		return value;
	}

	/**
	 * 事件函数
	 */
	quest.event = function(params,id){
		var heightName = $decode.getDeCodeNameByName("height");
		var weightName = $decode.getDeCodeNameByName("weight");
		var $height = $("#" + id).find("input[type=text][name='" + heightName + "']");
		var $weight = $("#" + id).find("input[type=text][name='" + weightName + "']");
		if( $height.length == 1 && $weight.length == 1 ){
			$height.unbind("input");
			$height.data("data",{"heightDom":$height,"weightDom":$weight,"id":id});
			$height.bind("input",onInput);
			$weight.unbind("input");
			$weight.data("data",{"heightDom":$height,"weightDom":$weight,"id":id});
			$weight.bind("input",onInput);
		}
	}

	/**
	 * 身高、体重改变时触发
	 */
	var onInput = function(){
		var data = $(this).data("data");
		var $height = data.heightDom;
		var $weight = data.weightDom;
		var h_dataUnit = $height.attr("data-dataUnit");
		var w_dataUnit = $weight.attr("data-dataUnit");
		var h_val = $height.val();
		var w_val = $weight.val();
		if( h_val && w_val ){
			if( h_dataUnit == "cm" ) h_val = h_val / 1000;
			if( w_dataUnit == "g" ) w_val = w_val / 1000;
			var val = ( h_val / w_val ).toFixed(2);
			$("#" + data.id + " .qaCard-whz").find(".form-control").length > 0 ? $("#" + data.id + " .qaCard-whz").find(".form-control").val(val) : $("#" + data.id + " .qaCard-whz").find(".form-control-static").html(val);
		}else{
			$("#" + data.id + " .qaCard-whz").find(".form-control").length > 0 ? $("#" + data.id + " .qaCard-whz").find(".form-control").val("") : $("#" + data.id + " .qaCard-whz").find(".form-control-static").html("");
		}
	}

	return quest;
});